<template>
  <a-modal
    title="回收印章"
    :visible="visible"
    class="normal-modal"
    :confirm-loading="loading"
    @cancel="cancel"
    @ok="submit"
  >
    <div
      v-loading="loading"
      class="page-detail"
    >
      <a-form-model
        ref="formRules"
        :model="form"
        :rules="formRules"
      >
        <a-row>
          <a-col span="12">
            <a-form-model-item
              label="印章状态"
              prop="sealStatus"
            >
              <a-radio-group
                v-model="form.sealStatus"
                default-value="完好"
              >
                <a-radio value="完好">
                  完好
                </a-radio>
                <a-radio value="损毁">
                  损毁
                </a-radio>
              </a-radio-group>
            </a-form-model-item>
          </a-col>
          <a-col span="12">
            <a-form-model-item
              label="回收时间"
              prop="recycleTime"
            >
              <a-date-picker
                v-model="form.recycleTime"
                value-format="x"
                :disabled-date="disabledDate"
              />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col span="12">
            <a-form-model-item
              label="上交人"
              prop="handInName"
            >
              <a-input-search
                v-model="form.handInName"
                @click="choosePerson(false)"
                @search="choosePerson(false)"
              />
            </a-form-model-item>
          </a-col>
          <a-col span="12">
            <a-form-model-item
              label="上交人电话"
              prop="handInPhone"
            >
              <b-input v-model="form.handInPhone" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col span="12">
            <a-form-model-item
              label="回收人"
              prop="recyleName"
            >
              <a-input-search
                v-model="form.recyleName"
                @click="choosePerson(true)"
                @search="choosePerson(true)"
              />
            </a-form-model-item>
          </a-col>
          <a-col span="12">
            <a-form-model-item
              label="回收人电话"
              prop="recyclePhone"
            >
              <b-input v-model="form.recyclePhone" />
            </a-form-model-item>
          </a-col>
        </a-row>
        <a-row>
          <a-col span="24">
            <a-form-model-item
              label="回收原因"
              prop="recyleReason"
            >
              <b-input v-model="form.recyleReason" />
            </a-form-model-item>
          </a-col>
          <a-col span="24">
            <a-form-model-item label="备注">
              <b-textarea v-model="form.remark" />
            </a-form-model-item>
          </a-col>
        </a-row>
      </a-form-model>
    </div>
    <SelectPerson
      :org-id="orgId"
      mode="single"
      :visible.sync="personVisible"
      @getSelectPerson="getSelectPerson"
    />
  </a-modal>
</template>

<script>
import projectApi from '@/api/office/seal/project'
import SelectPerson from '@/components/Select/SelectPerson'
import moment from 'moment'

export default {
    name: 'RecycleSeal',
    components: {
        SelectPerson
    },
    props: {
        visible: {
            type: Boolean,
            default: false
        },
        rowId: {
            type: String,
            default: ''
        }
    },
    data() {
        return {
            personVisible: false,
            loading: false,
            orgId: '',
            form: {
                sealStatus: '',
                baseId: '',
                recycleId: this.$userInfo().id,
                recyleName: this.$userInfo().name,
                recyclePhone: this.$userInfo().employee.phone,
                handInId: this.$userInfo().id,
                handInName: this.$userInfo().name,
                handInPhone: this.$userInfo().employee.phone,
                recyleReason: '',
                remark: ''
            },
            row: {},
            formRules: {
                recycleTime: { required: true, message: '回收时间不能为空', trigger: 'change' },
                handInName: { required: true, message: '上交人不能为空', trigger: 'change' },
                handInPhone: { required: true, message: '上交人电话不能为空', trigger: 'change' },
                recyleName: { required: true, message: '回收人不能为空', trigger: 'blur' },
                recyclePhone: { required: true, message: '回收人电话不能为空', trigger: 'blur' },
                recyleReason: { required: true, message: '回收原因不能为空', trigger: 'change' }
            },
            isRecycle: false
        }
    },
    watch: {
        visible(e) {
            this.getBaseInfo()
            if (!e) {
                this.$refs.formRules.resetFields()
                this.form = {
                    sealStatus: '',
                    baseId: '',
                    recycleId: this.$userInfo().id,
                    recyleName: this.$userInfo().name,
                    recyclePhone: this.$userInfo().employee.phone,
                    handInId: this.$userInfo().id,
                    handInName: this.$userInfo().name,
                    handInPhone: this.$userInfo().employee.phone,
                    recyleReason: '',
                    remark: ''
                }
            }
        }
    },
    created() {
    },
    methods: {
        disabledDate(current) {
            if (this.row.recycleStatus === '分公司已回收') {
                let date1 = this.row.recycleSeals[0]['recycleTime']
                return current < moment(date1).endOf('day')
            }
            if (this.row.recycleStatus === '使用中' || this.row.recycleStatus === '未回收') {
                return false
            }
            return true
        },
        getSelectPerson(list) {
            let e = list[0]
            // return this.$console(e)
            if (this.isRecycle) {
                this.form.recycleId = e.userId
                this.form.recyclePhone = e.phone
                this.form.recyleName = e.name
            } else {
                this.form.handInId = e.userId
                this.form.handInPhone = e.phone
                this.form.handInName = e.name
            }
        },
        choosePerson(isRecycle) {
            this.isRecycle = isRecycle
            if (this.row.recycleStatus === '分公司已回收') {
                if (isRecycle) {
                    this.orgId = ''
                } else {
                    this.orgId = this.row.branchComId
                }
            } else {
                if (isRecycle) {
                    this.orgId = this.row.branchComId
                } else {
                    this.orgId = this.row.depId
                }
            }
            this.personVisible = true
        },
        async getBaseInfo() {
            this.loading = true
            let res = await projectApi.findById({}, { id: this.rowId })
            if (res.code === this.SUCCESS_CODE) {
                this.row = res.data
                this.form.baseId = res.data.id
                this.form.sealStatus = res.data.sealStatus
                if (this.row.recycleStatus === '未回收') {
                    this.form.handInDepId = this.row.depId
                    this.form.handInDepName = this.row.depName
                } else {
                    this.form.handInDepId = this.row.branchComId
                    this.form.handInDepName = this.row.branchComName

                    if (this.row.recycleSeals) {
                        this.form.handInId = this.row.recycleSeals[0].handInId
                        this.form.handInPhone = this.row.recycleSeals[0].handInPhone
                        this.form.handInName = this.row.recycleSeals[0].handInName
                    }
                }
            }
            this.loading = false
        },
        async submit() {
            this.$refs.formRules.validate(async valid => {
                if (valid) {
                    this.$submitConfirm(async () => {
                        this.loading = true
                        let res = await projectApi.recycleAdd(this.form)
                        this.loading = false
                        if (res.code === this.SUCCESS_CODE) {
                            this.$noticeSuccess('提交成功')
                            this.cancel()
                            this.$emit('ok')
                        }
                    }, '是否确定要提交回收信息，提交后不可再次修改')
                } else {
                    this.$noticeWarning('请填写完整')
                }
            })

        },
        cancel() {
            this.$emit('update:visible', false)
        }
    }
}
</script>

<style scoped>

</style>
